package com.example.forum.services.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.MessageMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Message;
import com.example.forum.model.User;
import com.example.forum.services.IMessageService;
import com.example.forum.services.IUserService;
import com.example.forum.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Stack;

@Slf4j
@Service
public class MessageServiceImpl implements IMessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private IUserService userService;

    @Override
    public void create(Message message) {
        if(null == message || message.getPostUserId()==null || message.getReceiveUserId()==null
                || StringUtil.isEmpty(message.getContent())){
            log.warn("传入的站内信参数不合法!");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验接收者是否存在
        User user = userService.selectById(message.getReceiveUserId());
        if(null == user ||user.getDeleteState()==1){
            log.error("接受用户不存在!");
            throw new ApplicationException(AppResult.failed(ResultCode.RECEIVER_NOT_EXIST));
        }
        //设置默认值,写入数据库
        message.setState((byte)0); //表示未读状态
        message.setDeleteState((byte)0);
        Date date = new Date();
        message.setCreateTime(date);
        message.setUpdateTime(date);

        int row = messageMapper.insertSelective(message);
        if(row!=1){
            log.warn("发送站内信失败!受影响行数不为1");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_SEND_MESSAGE));
        }

    }

    @Override
    public Integer selectUnreadCount(Long receiveUserId) {
        if(null==receiveUserId || receiveUserId<=0){
            log.warn("传入的接收方id异常!");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        User user = userService.selectById(receiveUserId);
        if(null == user){
            log.warn("找不到对应接收用户! receiveUserId:{}",receiveUserId);
            throw new ApplicationException(AppResult.failed(ResultCode.RECEIVER_NOT_EXIST));
        }
        Integer count = messageMapper.selectUnreadCount(receiveUserId);
        //正常不会出现null
        if(null == count){
            log.warn("服务器内部异常!");
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        return count;
    }

    @Override
    public Message selectById(Long id) {
        if(null==id || id<=0 ){
            log.warn("参数异常!");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message message = messageMapper.selectByPrimaryKey(id);
        if(null == message  || message.getDeleteState()==1){
            log.warn("短信不存在");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXIST));
        }
        return message;
    }

    @Override
    public List<Message> selectByReceiveUserId(Long receiveUserId) {
        if(null==receiveUserId || receiveUserId<=0){
            log.warn("传入的接收方id异常!");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        User user = userService.selectById(receiveUserId);
        if(null == user){
            log.warn("找不到对应接收用户! receiveUserId:{}",receiveUserId);
            throw new ApplicationException(AppResult.failed(ResultCode.RECEIVER_NOT_EXIST));
        }
        return messageMapper.selectByReceiveUserId(receiveUserId);
    }

    @Override
    public void updateStateById(Long id, Byte state) {
        if(null==id || id<=0 || state <0 || state > 2){
            log.warn("参数异常!");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Message message = messageMapper.selectByPrimaryKey(id);
        if(null == message  || message.getDeleteState()==1){
            log.warn("短信不存在");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXIST));
        }

        Message updateMessage =new Message();
        updateMessage.setId(id);
        updateMessage.setState(state);
        updateMessage.setUpdateTime(new Date());
        int row=messageMapper.updateByPrimaryKeySelective(updateMessage);
        if(1 != row){
            log.warn("服务器内部异常!");
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }

    }

    @Override
    public void reply(Long repliedId, Message message) {
        if(null == repliedId || repliedId <=0){
            log.warn("参数异常!");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //校验站内信
        Message existsMessage = messageMapper.selectByPrimaryKey(repliedId);
        if(null == existsMessage || existsMessage.getDeleteState()==1){
            log.warn("站内信不存在! repliedId:{}",repliedId);
            throw new ApplicationException(AppResult.failed(ResultCode.MESSAGE_NOT_EXIST));
        }
        //更新状态
        updateStateById(repliedId,(byte) 2);//已回复
        //写入数据库
        create(message);
    }
}
